package com.itcat.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcat.api.mysql.UmsMenuApi;
import com.itcat.common.CommonPage;
import com.itcat.interceptor.UserHolder;
import com.itcat.mysql.pojo.UmsMenu;
import com.itcat.mysql.vo.UsmMenuVo;
import com.itcat.service.UsmMenuService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * [一句话描述该类的功能]
 *
 * @author : [Cr Mr]
 * @version : [v1.0]
 * @date : [2022/4/3 9:01]
 */
@Service
public class UsmMenuServiceImpl implements UsmMenuService {

    @DubboReference
    private UmsMenuApi umsMenuApi;

    @Autowired
    private AdminCacheServiceImpl service;

    /***
     * 树形结构返回所有菜单列表
     * @author Cr Mr
     * @date 2022/4/3 9:02
     */
    @Override
    public List<UsmMenuVo> treeList() {
        List<UmsMenu> menuAll = umsMenuApi.getMenuAll();
        long l = System.currentTimeMillis();
        List<UsmMenuVo> list = getTree(menuAll, 0L);
        return list;
    }

    /***
     * 递归查询子节点
     * @author Cr Mr
     * @date 2022/4/3 10:25
     */
    private List<UsmMenuVo> getTree(List<UmsMenu> menuAll, Long parentId) {
        if (menuAll != null) {
            List<UsmMenuVo> list = new ArrayList<>();
            menuAll.forEach(umsMenu -> {
                if (umsMenu.getParentId().equals(parentId)) {
                    UsmMenuVo usmMenuVo = UsmMenuVo.getUsmMenuVo(umsMenu);
                    usmMenuVo.setChildren(getTree(menuAll, usmMenuVo.getId()));
                    list.add(usmMenuVo);
                }
            });
            return list;
        }
        return null;
    }

    /***
     * 分页查询后台菜单
     * @author Cr Mr
     * @date 2022/4/3 16:10
     */
    @Override
    public CommonPage<UmsMenu> list(Long parentId, Integer pageNum, Integer pageSize) {
        Page<UmsMenu> selectPage = umsMenuApi.selectPage(parentId, pageNum, pageSize);
        CommonPage<UmsMenu> page = CommonPage.restPage(selectPage);
        return page;
    }

    /***
     * 添加后台菜单
     * @author Cr Mr
     * @date 2022/4/3 16:28
     */
    @Override
    public void create(UmsMenu umsMenu) {
        umsMenuApi.create(umsMenu);
        service.removeAdmin(UserHolder.get());
    }

    /***
     * 根据ID删除后台菜单
     * @author Cr Mr
     * @date 2022/4/3 16:34
     */
    @Override
    public void delete(Long id) {
        umsMenuApi.delete(id);
        service.removeAdmin(UserHolder.get());
    }

    /***
     * 修改后台菜单
     * @author Cr Mr
     * @date 2022/4/3 16:55
     */
    @Override
    public void update(Long id, UmsMenu umsMenu) {
        umsMenuApi.update(id, umsMenu);
        service.removeAdmin(UserHolder.get());
    }

    /***
     * 修改菜单显示状态
     * @author Cr Mr
     * @date 2022/4/3 17:07
     */
    @Override
    public void updateHidden(Long id, Integer hidden) {
        umsMenuApi.updateHidden(id, hidden);
        service.removeAdmin(UserHolder.get());
    }

    /***
     * 根据ID获取菜单详情
     * @author Cr Mr
     * @date 2022/4/3 17:07
     */
    @Override
    public UmsMenu getUmsMenu(Long id) {
        return umsMenuApi.getUmsMenu(id);
    }

}
